/*
 * Sonatype Nexus (TM) Open Source Version
 * Copyright (c) 2008-present Sonatype, Inc.
 * All rights reserved. Includes the third-party code listed at http://links.sonatype.com/products/nexus/oss/attributions.
 *
 * This program and the accompanying materials are made available under the terms of the Eclipse Public License Version 1.0,
 * which accompanies this distribution and is available at http://www.eclipse.org/legal/epl-v10.html.
 *
 * Sonatype Nexus (TM) Professional Version is available from Sonatype, Inc. "Sonatype" and "Sonatype Nexus" are trademarks
 * of Sonatype, Inc. Apache Maven is a trademark of the Apache Software Foundation. M2eclipse is a trademark of the
 * Eclipse Foundation. All other trademarks are the property of their respective owners.
 */
package org.sonatype.nexus.repository.content.upgrades;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Arrays;

import org.sonatype.goodies.testsupport.TestSupport;
import org.sonatype.nexus.datastore.api.DataSession;
import org.sonatype.nexus.repository.Format;
import org.sonatype.nexus.kv.GlobalKeyValueStore;
import org.sonatype.nexus.repository.content.store.ComponentDAO;
import org.sonatype.nexus.repository.content.tasks.normalize.NormalizeComponentVersionTaskDescriptor;
import org.sonatype.nexus.scheduling.UpgradeTaskScheduler;
import org.sonatype.nexus.scheduling.TaskScheduler;
import org.sonatype.nexus.testdb.DataSessionRule;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;

import static java.lang.String.format;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.sonatype.nexus.datastore.api.DataStoreManager.DEFAULT_DATASTORE_NAME;

public class ComponentNormalizedVersionMigrationStepTest
    extends TestSupport
{
  private final String CREATE_COMPONENT_TABLE = "CREATE TABLE IF NOT EXISTS {format}_component (" +
      "component_id INT GENERATED BY DEFAULT AS IDENTITY" +
      ");";

  @Rule
  public DataSessionRule sessionRule = new DataSessionRule(DEFAULT_DATASTORE_NAME)
      .access(ComponentDAO.class);

  @Mock
  private UpgradeTaskScheduler upgradeTaskScheduler;

  @Mock
  private GlobalKeyValueStore globalKeyValueStore;

  @Mock
  private TaskScheduler scheduler;

  @Mock
  private Format maven2;

  @Mock
  private Format nuget;

  private DataSession<?> session;

  private ComponentNormalizedVersionMigrationStep migrationStep;

  @Before
  public void setUp() {
    when(maven2.getValue()).thenReturn("maven2");
    when(nuget.getValue()).thenReturn("nuget");

    migrationStep =
        new ComponentNormalizedVersionMigrationStep(Arrays.asList(nuget, maven2), globalKeyValueStore, scheduler,
            upgradeTaskScheduler);

    session = sessionRule.openSession(DEFAULT_DATASTORE_NAME);
  }

  @After
  public void cleanup() {
    session.close();
  }

  /*
   * Verifies that the order of formats doesn't affect the generated checksum
   */
  @Test
  public void testChecksum_order() {
    ComponentNormalizedVersionMigrationStep migrationStep2 =
        new ComponentNormalizedVersionMigrationStep(Arrays.asList(maven2, nuget), globalKeyValueStore, scheduler,
            upgradeTaskScheduler);

    assertThat(migrationStep.getChecksum(), is(migrationStep2.getChecksum()));
  }

  /*
   * Verifies that a different list of formats results in a different checksum
   */
  @Test
  public void testChecksum_different() {
    ComponentNormalizedVersionMigrationStep migrationStep2 =
        new ComponentNormalizedVersionMigrationStep(Arrays.asList(nuget), globalKeyValueStore, scheduler,
            upgradeTaskScheduler);

    assertThat(migrationStep.getChecksum(), not(migrationStep2.getChecksum()));
  }

  @Test
  public void testFormatsAreAltered() throws Exception {
    createTable("maven2");
    createTable("nuget");
    try (Connection connection = sessionRule.openConnection(DEFAULT_DATASTORE_NAME)) {
      migrationStep.migrate(connection);
      assertTrue(columnExists(connection, "maven2", "normalized_version"));
      assertTrue(columnExists(connection, "nuget", "normalized_version"));
    }
  }

  @Test
  public void testTaskIsScheduled() throws Exception {
    try (Connection connection = sessionRule.openConnection(DEFAULT_DATASTORE_NAME)) {
      migrationStep.migrate(connection);
      verify(scheduler).createTaskConfigurationInstance(eq(NormalizeComponentVersionTaskDescriptor.TYPE_ID));
      verify(upgradeTaskScheduler).schedule(any());
    }
  }

  private void createTable(final String format) throws Exception {
    try (Connection connection = sessionRule.openConnection(DEFAULT_DATASTORE_NAME)) {
      PreparedStatement ps = connection.prepareStatement(CREATE_COMPONENT_TABLE.replaceAll("\\{format\\}", format));
      ps.execute();
    }
  }

  private boolean columnExists(Connection conn, String format, String columnName) throws Exception {
    String sql = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE UPPER(TABLE_NAME) = ? AND UPPER(COLUMN_NAME) = ?";
    try (PreparedStatement statement = conn.prepareStatement(sql)) {
      statement.setString(1, format("%s_component", format).toUpperCase());
      statement.setString(2, columnName.toUpperCase());
      try (ResultSet results = statement.executeQuery()) {
        return results.next();
      }
    }
  }
}
